

// build.js
const fs = require('fs');
const path = require('path');

// 1. 执行Vite构建
require('child_process').execSync('vite build', { stdio: 'inherit' });

// 2. 重命名文件
// const distPath = path.join(__dirname, 'dist');
// fs.renameSync(
//   path.join(distPath, 'index.html'),
//   path.join(distPath, 'index.cshtml')
// );
 

const { promisify } = require('util');
const rename = promisify(fs.rename);
const mkdir = promisify(fs.mkdir);
const readdir = promisify(fs.readdir);
const stat = promisify(fs.stat);
const copyFile = promisify(fs.copyFile);
const rm = promisify(fs.rm);
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);

async function organizeFiles() {
  const distPath = path.resolve(__dirname, 'dist');
  
  // 定义目标路径
  const targetPaths = {
    viewDir: path.join(distPath, 'areas', 'HT', 'Views', 'HTPS'),
    contentDir: path.join(distPath, 'Content', 'HT'),
    assetsDir: path.join(distPath, 'Content', 'HT', 'assets')
  };

  try {
    // 创建所有需要的目录（包括多级子目录）
    await Promise.all([
      mkdir(targetPaths.viewDir, { recursive: true }),
      mkdir(targetPaths.contentDir, { recursive: true }),
      mkdir(targetPaths.assetsDir, { recursive: true })
    ]);
// 1. 拷贝 index.html 到 /Content/HT
    const htmlSource = path.join(distPath, 'index.html');
    const htmlDest = path.join(targetPaths.viewDir, 'index.cshtml');
    
    


    const htmlCopyDest = path.join(targetPaths.contentDir, 'index.html');
    if (fs.existsSync(htmlSource)) {
      await copyFile(htmlSource, htmlCopyDest);
      console.log(`已拷贝 index.html 到 ${htmlCopyDest}`);
    }
    // 1. 读取原始index.html内容
    let htmlContent = '';
    if (fs.existsSync(htmlSource)) {
      htmlContent = await readFile(htmlSource, 'utf8');
      
      // 2. 修改资源路径，添加 ~/Content/HT 前缀
      htmlContent = htmlContent
        .replace(/(href|src)=".\/assets\//g, '$1="~/Content/HT/assets/');
      
      // 3. 写入修改后的内容到 /Content/HT/index.html
      const htmlCopyDest = path.join(targetPaths.viewDir, 'index.cshtml');
      await writeFile(htmlCopyDest, htmlContent);
      console.log(`已创建并修改 ${htmlCopyDest} 资源路径`);
    }
    //htmlSource = path.join(distPath, 'index.html');
    // 移动并重命名 index.html → index.cshtml

    // if (fs.existsSync(htmlSource)) {
    //   await rename(htmlSource, htmlDest);
    //   console.log(`已移动 index.html 到 ${htmlDest}`);
    // }

    // 移动 favicon.ico
    const faviconSource = path.join(distPath, 'favicon.ico');
    const faviconDest = path.join(targetPaths.contentDir, 'favicon.ico');
    
    if (fs.existsSync(faviconSource)) {
      await rename(faviconSource, faviconDest);
      console.log(`已移动 favicon.ico 到 ${faviconDest}`);
    }

    // 移动 assets 目录及其子目录
    const assetsSource = path.join(distPath, 'assets');
    if (fs.existsSync(assetsSource)) {
      await copyDir(assetsSource, targetPaths.assetsDir);
      await rm(assetsSource, { recursive: true });
      console.log(`已移动 assets 目录到 ${targetPaths.assetsDir}`);
    }

    console.log('文件结构重组完成！');
  } catch (err) {
    console.error('重组文件结构时出错:', err);
    process.exit(1);
  }
}

// 递归复制目录
async function copyDir(src, dest) {
  await mkdir(dest, { recursive: true });
  const entries = await readdir(src, { withFileTypes: true });

  await Promise.all(entries.map(async entry => {
    const srcPath = path.join(src, entry.name);
    const destPath = path.join(dest, entry.name);

    if (entry.isDirectory()) {
      await copyDir(srcPath, destPath);
    } else {
      await copyFile(srcPath, destPath);
    }
  }));
}

organizeFiles();